/**
 * Copyright (C) 2008+ Spark Zheng
 *
 * @file	src/timer.c
 * @brief
 *      timer module include the operations of:
 *	-1. Wall time: (time_t <=> struct tm)
 *	-2. System time: struct timeval / clock (RTC, PIT, TSC)
 *	-3. Kernel jiffies (linux/time.h)
 *	-4. signal -- SIGALRM (struct timespec) / setitimer(getitimer)
 *	-5. sleep/usleep/nanosleep (struct timespec)
 *	if need more precision, think about __volatile__ __asm__("nop");
 *
 * @see	 dlib
 * @module      dlib
 *
 * @history
 *      version 0.0.1   Spark Zheng init timer module
 */

#include <stdint.h>
#include "timer.h"

inline int dlib_day_span(struct tm *from, struct tm *to)
{
	time_t from_tick;
	time_t to_tick;

	if (from == NULL) {
		from_tick = time(NULL);
	} else {
		from_tick = mktime(from);
	}
	
	if (to == NULL) {
		to_tick = time(NULL);
	} else {
		to_tick = mktime(to);
	}
	
	return (to_tick - from_tick) / 24 / 3600;
}

inline int dlib_day_is_between(struct tm *f, struct tm *t, int32_t n)
{
	time_t now;
	time_t then;

	if (f == NULL || n == 0) {
		return 1;
	}
	
	if (t == NULL) {
		now = time(NULL);
	} else {
		now = mktime(t);
	}
	then = mktime(f);
	
	if ((now - then) > (n * 24 * 3600))
		return 1;
	else
		return 0;
}


/**
 * min heap based timer implementation
 * use heap module
 * @warning static timer pool, all timer in Global heap
 */

